#include "server.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

void bind_and_listen(int server_sock) {
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(SERVER_PORT);

    if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
        perror("绑定失败");
        close(server_sock);
        exit(1);
    }

    if (listen(server_sock, MAX_LISTENS) == -1) {
        perror("监听失败");
        close(server_sock);
        exit(1);
    }

    printf("服务器正在监听端口 %d\n", SERVER_PORT);
}